package DataStructures.linkedlist;

import java.util.List;

/**
 * @author ：ALi
 * @date ：Created in 2021/9/25 15:59
 * @description：双链表练习
 * @modified By：ALi
 * @version: 1.0$
 */

public class DoubleLinkedListDemo {
    public static void main(String[] args){
        System.out.println("双向链表的测试");
        //创建节点对象
        HeroNode2 hero1 = new HeroNode2(1,"松江1","及时雨1");
        HeroNode2 hero2= new HeroNode2(3,"松江3","及时雨2");
        HeroNode2 hero3 = new HeroNode2(5,"松江5","及时雨3");
        HeroNode2 hero4 = new HeroNode2(7,"松江7","及时雨4");
        HeroNode2 hero5 = new HeroNode2(5,"松江6","及时雨6");
        HeroNode2 hero6 = new HeroNode2(6,"松江77","及时雨77");

        SingleLinkedList2 list = new SingleLinkedList2();
        list.add(hero1);
        list.add(hero2);
        list.add(hero3);
        list.add(hero4);
        list.add(hero5);

        list.show();
        list.delect(hero4);
        System.out.println("删除后的");
        list.show();
        list.updata(hero5,"5");

        System.out.println("修改后的");
        list.show();
    }


}

/**
 * 接点类
 */
class HeroNode2{
    public int no;
    public String Name;
    public String nickName;
    public HeroNode2 next;
    public HeroNode2 pro;

    //构造器
    public HeroNode2(int no, String Name, String nickName){
        this.no = no;
        this.Name = Name;
        this.nickName = nickName;
    }

    //重写toString方法
    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", Name='" + Name + '\'' +
                ", nickName='" + nickName + '\'' +
                '}';
    }

}

/**
 * admin list
 */
class SingleLinkedList2 {
    //创建头结点,不存数据(带头节点的链表)
    private HeroNode2 head = new HeroNode2(0, "", "");

    /**
     * get方法
     *
     * @return 头结点
     */
    public HeroNode2 getHead() {
        return head;
    }


    /**
     * 打印链表
     */
    public void show(){
        if (head.next == null){
            System.out.println("链表为空");
//            throw new RuntimeException("链表为空！");
        }
        HeroNode2 temp = head.next;
        while(temp != null){
            System.out.println(temp);
            temp = temp.next;
        }

    }
    /**
     * 增加新节点
     * @param node 新节点node
     */
    public void add(HeroNode2 node){
        //创建临时节点，用它来代替原来的头结点，保证原来的链表是不变的。单链表有了头结点，其他节点就都有了。
        HeroNode2 temp = head;
        //自己总是搞忘了！
        //找到最后一个节点，然后插入
        while(true){
            if (temp.next == null) {
                break;
            }
            temp = temp.next;

        }
        //此时temp指向链表的最后
        temp.next = node;
        node.pro = temp;

    }
    /**
     *更新节点
     * @param node:要修改的节点
     * @param name:修改节点后的名字
     */
    public void updata(HeroNode2 node,String name){
        if (head.next == null){
            return;
        }
        HeroNode2 temp = head.next;
        boolean res = true;
        while(temp.no != node.no){
            if (temp.next == null){
                System.out.printf("没找到节点%s\n",node.Name);
                res = false;
                break;
            }
            temp = temp.next;
        }
        if(res){
            temp.Name = name;
        }else
            return;

    }

    /**
     * 删除节点
     * @param delectNode 要删除的节点
     */
    public void delect(HeroNode2 delectNode){
        HeroNode2 node = head;
        if (head.next == null){
            System.out.println("链表为空");
            return;
        }
        boolean res = true;
        while (res){
            //到了节点的最后
            if (node.next == null){
                System.out.printf("无%s节点\n",delectNode.Name);
                break;
            }
            if (node.next.no == delectNode.no){
                //就算要删除的节点是尾结点也可以删除
//                node.next = node.next.next;//这个也可以
                node.next = delectNode.next;
                if(delectNode.next!=null){
                    delectNode.next.pro = node;
                }
                res = false;
                break;
            }
            node = node.next;
        }
    }

}